= render 'shared/header_big'
.w-row.admin.use-sort
  h1.fontsize-largest
    = @title = t('admin.management.title')
    ' :
    = @total_projects
    '
    = t('admin.management.total')

  h3 = t('admin.filter.title')
  = simple_form_for '', url: admin_projects_path, method: :get do |form|
    .w-row
      = form.input :order_by, as: :hidden, input_html: {value: params[:order_by], class: 'w-input text-field'}
      .w-col-3.w-col
        = form.input :pg_search, as: :string, label: t('admin.filter.pg_search'), input_html: {value: params[:pg_search], class: 'w-input text-field'}
      .w-col-3.w-col
        = form.input :user_name_contains, as: :string, label: t('admin.filter.user_name_contains'), input_html: {value: params[:user_name_contains], class: 'w-input text-field'}
      .w-col-2.w-col
        = form.input :by_user_email, as: :string, label: t('.by_user_email'), input_html: {value: params[:by_user_email], class: 'w-input text-field'}
      .w-col-2.w-col
        = form.input :with_state, collection: Project.state_names, label: t('admin.filter.with_state'), as: :select, selected: params[:with_state], include_blank: true, input_html: { class: 'w-input text-field' }
    .w-row
      - [:between_created_at, :between_expires_at, :between_online_date, :between_updated_at, :goal_between].each_with_index do |between_field, index|
        - mask = (between_field == :goal_between ? nil : '99/99/9999')
        - if index % 3 == 0
          .w-row
        .w-col-2.w-col
          = form.input "#{between_field}[start_at]", as: :string, label: t(".#{between_field}"), input_html: {value: params[between_field] && params[between_field][:start_at], data: { fixed_mask: mask } , class: 'w-input text-field'}
        .w-col-2.no-label.w-col
          = form.input "#{between_field}[ends_at]", as: :string, label: false, input_html: { value: params[between_field] && params[between_field][:ends_at], data: { fixed_mask: mask } , class: 'w-input text-field'}

      .w-col-2.no-label.w-col
        = form.button :submit, t('admin.filter.submit'), class: 'btn btn-medium'

    .w-row
      .w-col-2.w-col-offset-10.w-col
        .fontsize-largest = @scoped_projects.count(:all)
        .fontsize-base
          = pluralize_without_number(@scoped_projects.count(:all), t('project_singular'), t('project_plural'))
          '
          = pluralize_without_number @scoped_projects.count(:all), t('found_singular'), t('found_plural')


  = paginate @projects
  - if @scoped_projects.count(:all) < 1
    = t('.no_projects')
  - else
    table.table.fontsize-smallest
      thead.card.card-secondary
        tr
          th.w-col-2.w-col= t('admin.table.name')
          th.w-col-1.w-col= t('admin.table.category')
          th.w-col-1.w-col
            a data-sort="goal" href='#'= t('.goal')
            span.sort-order
          th.w-col-1.w-col
            a data-sort="project_totals.progress" href='#'= t('.pledged')
            span.sort-order
          th.w-col-1.w-col
            a data-sort="project_totals.total_contributions" href='#'= t('.total_contributions')
            span.sort-order
          th.w-col-1.w-col= t('admin.table.recommended')
          th.w-col-1.w-col
            a data-sort="online_date" href='#'= t('.online_date')
            span.sort-order
          th.w-col-1.w-col
            a data-sort="projects.expires_at" href='#'= t('.expires_at')
            span.sort-order
          th.w-col-1.w-col
            a data-sort="updated_at" href='#'= t('.last_modification')
            span.sort-order
          th.w-col-1.w-col= t('admin.table.state')
          th.w-col-1.w-col
      tbody
        - @projects.includes(:category).each do |project|
          tr id=project.id class=cycle("card", "card card-secondary")
            td.w-col-2.w-col= link_to project.name, project_by_slug_path(permalink: project.permalink)
            td.w-col-1.w-col= project.category
            td.w-col-1.w-col= best_in_place [:admin, project], :goal, method: :put, type: :input, display_as: :display_goal
            td.w-col-1.w-col= "#{project.display_pledged_with_cents} (#{project.progress}%)"
            td.w-col-1.w-col= project.total_contributions
            td.w-col-1.w-col= best_in_place [:admin, project], :recommended, method: :put, type: :checkbox, collection: [t('no'), t('yes')]
            td.w-col-1.w-col= project.online_date.strftime('%d/%m/%Y') if project.online_date
            td.w-col-1.w-col= project.display_expires_at
            td.w-col-1.w-col= project.updated_at.strftime('%d/%m/%Y')
            td.w-col-1.w-col
              .w-dropdown-toggle.dropdown-toggle.project-admin-menu
                - if project.online?
                    span.label.label-online= project.state
                - elsif project.successful?
                  span.label.label-success= project.state
                - elsif project.failed? or project.rejected?
                  span.label.label-important= project.state
                - elsif project.waiting_funds?
                  span.label.label-warning= project.state
                - else
                  span.label= project.state
              nav.w-dropdown-list.dropdown-list.user-menu
                - if project.can_push_to_online?
                  li
                    = link_to push_to_online_admin_project_path(project), class: 'w-dropdown-link dropdown-link fontsize-smaller', method: :put do
                      = t('admin.table.put_online')
                - if project.can_approve?
                  li
                    = link_to approve_admin_project_path(project), class: 'w-dropdown-link dropdown-link fontsize-smaller', method: :put do
                      = t('admin.table.approve')

                - if project.can_reject?
                  li
                    = link_to reject_admin_project_path(project), class: 'w-dropdown-link dropdown-link fontsize-smaller', method: :put  do
                      = t('admin.table.reject')

                - if project.can_push_to_draft?
                  li
                    = link_to  push_to_draft_admin_project_path(project), class: 'w-dropdown-link dropdown-link fontsize-smaller', method: :put  do
                      = t('admin.table.send_to_draft')
                - if project.can_push_to_trash?
                  li
                    = link_to  push_to_trash_admin_project_path(project),class: 'w-dropdown-link dropdown-link fontsize-smaller', data:{ confirm: t('admin.table.confirmation')}, method: :put  do
                      = t('admin.table.send_to_trash')


  = paginate @projects
= render 'shared/footer_big'
